import 'dart:math';

import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/provider/global_provider.dart';
import 'package:flutter_elf/widgets/appbar_creator.dart';
import 'package:provider/provider.dart';

class ThemePage extends BaseWidget{
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return ThemePageState();
  }
}

class ThemePageState extends BaseWidgetState<ThemePage>{

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_){
      getColor();
    });
  }

  getColor(){
    Provider.of<GlobalProvider>(context).addListener((){
      globalPrint('颜色改变1');
    });
    MaterialColor c = Provider.of<GlobalProvider>(context).getPrimaryColor();
    globalPrint('颜色改变2' + c.toString());

  }

  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildView
    return Column(
      children: <Widget>[
        RaisedButton(onPressed: (){
          Provider.of<GlobalProvider>(context).setAppColor(MaterialColor(randomColor().value,{}));
        },child: Container(
          child: Text('点击修改主题色'),
        ),)
      ],
    );
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return AppBarCreator.create(context, '修改主题色');
  }

  Color randomColor(){
    return Color.fromRGBO(
        randomInt(255), randomInt(255), randomInt(255), 1.0);
  }

  randomInt(num){return Random().nextInt(num);}
}